package com.ly.emp.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.emp.bean.dto.HubTeamWorkDTO;
import com.ly.emp.constant.EmpErrorConstants;
import com.ly.emp.entity.HubTeamWork;
import com.ly.emp.mapper.HubTeamWorkMapper;
import com.ly.emp.service.IHubTeamWorkService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 工单-impl
 *
 * @author luoyun
 * @Date: 2025-05-20
 */
@Service
public class HubTeamWorkServiceImpl extends ServiceImpl<HubTeamWorkMapper, HubTeamWork> implements IHubTeamWorkService {

    @Override
    public R getId(String id) {
        HubTeamWork entity = this.getById(id);
        if (entity == null) {
            return R.fail("未找到对应数据");
        }
        return R.ok(entity);
    }

    @Override
    public R getList(HubTeamWork entity) {
        List<HubTeamWork> list = this.list(
                new LambdaQueryWrapper<HubTeamWork>()
                        .eq(StrUtil.isNotEmpty(entity.getOrgCode()), HubTeamWork::getOrgCode, entity.getOrgCode())
        );
        return R.ok(list);
    }

    @Override
    public R getPage(HubTeamWork entity, Page<HubTeamWork> page) {
        String orgCode = entity.getOrgCode();
        if(StrUtil.isBlank(orgCode)){
            return R.fail(EmpErrorConstants.NO_TEAM);
        }
        IPage<HubTeamWork> pageList = this.page(
                page,
                new LambdaQueryWrapper<HubTeamWork>()
                        .eq(HubTeamWork::getOrgCode,orgCode)
                        .eq(StrUtil.isNotBlank(entity.getPeriodCode()), HubTeamWork::getPeriodCode,entity.getPeriodCode())
                        .eq(StrUtil.isNotBlank(entity.getWorkTypeCode()), HubTeamWork::getWorkTypeCode,entity.getWorkTypeCode())
                        .like(StrUtil.isNotBlank(entity.getWorkItemName()), HubTeamWork::getWorkItemName,entity.getWorkItemName())
                        .like(StrUtil.isNotBlank(entity.getWorkTypeName()), HubTeamWork::getWorkTypeName,entity.getWorkTypeName())
                        .orderByDesc(HubTeamWork::getPeriodCode)
                        .orderByAsc(HubTeamWork::getDisplayOrder)
        );
        return R.ok(pageList);
    }

    @Override
    public R add(HubTeamWork entity) {
        boolean flag = this.save(entity);
        return flag ? R.ok(true, "创建成功") : R.fail("创建失败");
    }

    @Override
    public R edit(HubTeamWork entity) {
        boolean flag = this.updateById(entity);
        return flag ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    @Override
    public R delete(String id) {
        boolean flag = this.removeById(id);
        return flag ? R.ok(true, "删除成功") : R.fail("删除失败");
    }

    @Override
    public R deleteBatch(String ids) {
        boolean flag = this.removeByIds(Arrays.asList(ids.split(",")));
        return flag ? R.ok(true, "批量删除成功") : R.fail("批量删除失败");
    }

    @Override
    public R updateBatch(CommonBatchUpdateDTO<HubTeamWork> dto) {
        List<HubTeamWork> datas = dto.getDatas();
        boolean flag = this.saveOrUpdateBatch(datas);
        return flag ? R.ok(true, "批量修改成功") : R.fail("批量修改失败");
    }

    @Override
    public R importHistory(HubTeamWorkDTO dto) {
        String periodCode = dto.getPeriodCode();
        String periodCode1 = dto.getPeriodCode1();
        List<HubTeamWork> list = this.list(
                new LambdaQueryWrapper<HubTeamWork>()
                        .eq(HubTeamWork::getPeriodCode,periodCode1)
                        .eq(HubTeamWork::getOrgCode,dto.getOrgCode())
        );
        if(CollUtil.isEmpty(list)){
            return R.ok(true,"当前选择的周期，暂无指标数据，请选择其它周期");
        }
        //判断是否已经添加
        List<HubTeamWork> saveList = new ArrayList<>();
        list.stream().forEach(n ->{
            //判断是否已经同步过，同步过则不再处理
            n.setId(null);
            n.setPeriodCode(periodCode);
            List<HubTeamWork> entityByNames = this.getEntityByNames(n);
            if(CollUtil.isEmpty(entityByNames)){
                saveList.add(n);
            }
        });
        boolean flag = this.saveOrUpdateBatch(saveList);
        return flag ? R.ok(true,"数据移入成功") : R.fail("数据已经存在，请勿重复移入");
    }

    /**
     * 根据名字判断是否已经存在
     * @param entity
     * @return
     */
    public List<HubTeamWork> getEntityByNames(HubTeamWork entity){
        return this.list(
                new LambdaQueryWrapper<HubTeamWork>()
                        .eq(HubTeamWork::getOrgCode,entity.getOrgCode())
                        .eq(HubTeamWork::getPeriodCode,entity.getPeriodCode())
                        .eq(HubTeamWork::getWorkTypeName,entity.getWorkTypeName())
                        .eq(HubTeamWork::getWorkItemName,entity.getWorkItemName())
        );
    }
}
